<?php
namespace app\wadmin\controller;

use think\facade\Db;
use app\BaseController;

class Upload extends BaseController
{

    public function index()
    {
        $file = request()->file('file');        
        try {
        // validate(['file'=>[
        //     'fileSize'=>40960000,
        //     ]])->check(['file' => $file]);
            //'fileExt'=>'gif,jpg,png']])->check(['file' => $file]);
        // 上传到本地服务器
        $p=input();
        $folder_name='';
        if(isset($p['type']))
        {
            $folder_name=$p['type'];
        }
        $savename = \think\facade\Filesystem::disk('public')->putFile($folder_name, $file);
        $savename = str_replace('\\', '/', '/'.$savename);
        $filepath='/uploads'.$savename;
        $channel=1;
        if(isset($p['channel']))
        {
            $channel=intval($p['channel']);
        }
        
        $ext_arr=explode('.', $savename);
        $ext = end($ext_arr);
        $name = $file->getOriginalName();
        $size=$file->getSize();
        $file_data=['filepath'=>$filepath,'channel'=>$channel,'create_time'=>time(),'originalname'=>$name,'size'=>$size,'ext'=>$ext];
        
        Db::name('file')->insert($file_data);
        return jsonReturn(1,'上传成功',['url'=>$filepath]);
        } catch (\think\exception\ValidateException $e) {
            return jsonReturn(0,$e->getMessage());
        }
    }
    
    //保存图片
    public function saveimg()
    {
        $p=input();
        $file =  array(
            'sn'=>isset($p['sn'])?$p['sn']:'',
            'order_id'=>isset($p['id'])?intval($p['id']):0,
            'filepath'=>$p['imgurl'],
            'memo'=>'',
            'size'=>0,
            'ext'=>'',
            'user_id'=>$this->admin['id'],
            'company_id'=>$this->admin['danwei_id'],
            'add_time'=>time()
        );
        Db::name('file')->insert($file);
    }
    //移除图片
    public function removeimg()
    {
        $p = input();
        \app\service\UploadService::remove($p);
    }
    public function upPostfile()
    {
        $file = request()->file('file');
        try {
        validate(['file'=>[
            'fileSize'=>10240000,
            'fileExt'=>'xls,xlsx']])->check(['file' => $file]);
        // 上传到本地服务器
        $savename = \think\facade\Filesystem::disk('public')->putFile('file', $file);
        $savename = str_replace('\\', '/', '/'.$savename);
        $imgPath='/uploads'.$savename;
        
        $ext_arr=explode('.', $savename);
        $ext = end($ext_arr);
        $name = $file->getOriginalName();
        
        //读取EXCEL
        require_once "../vendor/PHPExcel/PHPExcel.php";
       
       $filePath=$_SERVER["DOCUMENT_ROOT"].$imgPath;
       //return json(['filepath'=>$filePath,'savename'=>$savename]);
       $ext=substr($filePath, strrpos($filePath, '.') + 1);
       
       return jsonReturn(1,'上传成功',['filepath'=>$imgPath]);
       //return ['code'=>1,'msg'=>'上传成功',['filepath'=>$imgPath]];
       
        } catch (\think\exception\ValidateException $e) {
            //return $this->error($e->getMessage());
            jsonReturn(0,'上传失败',['data'=>$e->getMessage()]);
        }
    }
}
